﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Workflow.Task;
using System.Data;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.Workflow.Form
{
    public class FlowFormDAL : BaseDAL
    {
        
        internal Dictionary<int, string> GetCurrentNodes(List<FlowFormInfo> forms)
        {
            
            Dictionary<int, string> nodes = new Dictionary<int, string>();
            string ids = "";
            foreach (FlowFormInfo form in forms)
            {
                ids += form.ID + ",";
            }
            ids = ids.Trim(',');
            if (ids != "")
            {
                string sql = "select distinct FormID,NodeID,b.Name as NodeName from " + WFTableName.FlowTask + " as a inner join FlowNode as b on a.NodeID = b.ID where a.FormID in (" + ids + ") and a.State !=" + (int)TaskState.已完成;
                DataTable dt = dbOper.Query(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    int formID = (int)dr["FormID"];
                    string nodeName=dr["NodeName"].ToString();
                    if (nodes.ContainsKey(formID))
                    {
                        nodes[formID] += "," + nodeName;
                    }
                    else
                    {
                        nodes.Add(formID, nodeName);
                    }
                }

            }

            return nodes;
        }

        internal void SetState(int id, FormState formState)
        {
            string sql = "update " + WFTableName.FlowForm + " set State = @p0 where ID=@p1";
            dbOper.RunSql(sql, (int)formState, id);
        }

        internal List<FlowFormInfo> GetList(IEnumerable<int> ids)
        {
            string strids = ConvertUtil.ConvertToDbStr(ids);
            string sql = "select * from " + WFTableName.FlowForm + " where ID in (" + strids + ")";
            List<FlowFormInfo> forms = dbOper.Query<FlowFormInfo>(sql);
            return forms;
        }
    }

}
